package com.book.dao.borrow.impl;

import com.book.dao.borrow.BorrowDao;
import com.book.pojo.Admin;
import com.book.pojo.Book;
import com.book.pojo.BorrowRecord;
import com.book.utils.DbUtils;


import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class BorrowDaoImpl implements BorrowDao {

    /**
     * 添加借阅记录（借书）
     */
    @Override
    public int addBorrowRecord(BorrowRecord borrowRecord) {
        boolean flag = true;
        String sql2 = "SELECT * FROM books WHERE id = ?";
        try(Connection conn= DbUtils.getConnection();
            PreparedStatement pstmt2 = conn.prepareStatement(sql2)){
            pstmt2.setLong(1, borrowRecord.getBookId());
            ResultSet rs2 = pstmt2.executeQuery();
            if (rs2.next()) {
                Book book = new Book();
                book.setId(rs2.getInt("id"));
                book.setTitle(rs2.getString("title"));
                book.setAuthor(rs2.getString("author"));
                book.setAvailable(rs2.getBoolean("available"));
                flag = book.isAvailable();
            }
        }catch (SQLException e) {
            e.printStackTrace();
        }
        if(flag == true){
            String sql = "INSERT INTO borrow_records (reader_id, book_id) VALUES (?, ?)";
            String sql1 = "UPDATE books SET available=0 WHERE id = ?";
            try (Connection conn = DbUtils.getConnection();
                 PreparedStatement pstmt = conn.prepareStatement(sql);
                 PreparedStatement pstmt1 = conn.prepareStatement(sql1)) {
                pstmt.setInt(1, borrowRecord.getReaderId());
                pstmt.setInt(2, borrowRecord.getBookId());
                pstmt1.setInt(1, borrowRecord.getBookId());
                pstmt1.executeUpdate();
                return pstmt.executeUpdate(); // 返回影响的行数
            } catch (SQLException e) {
                e.printStackTrace();
            }
            return 0;
        }else{
            System.out.println("这本书不能借阅");
        }
        return 0;
    }

    /**
     * 归还书籍（更新归还日期）
     */
    @Override
    public int returnBook(int borrowRecordId) {
        String sql = "UPDATE borrow_records SET return_date = NOW() WHERE book_id = ? AND return_date IS NULL";
        String sql1 = "UPDATE books SET available=1 WHERE id = ?";
        try (Connection conn = DbUtils.getConnection();
             PreparedStatement pstmt = conn.prepareStatement(sql);
             PreparedStatement pstmt1 = conn.prepareStatement(sql1)) {
            pstmt.setInt(1, borrowRecordId);
            pstmt1.setInt(1, borrowRecordId);
            pstmt1.executeUpdate();
            return pstmt.executeUpdate(); // 返回影响的行数
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

    /**
     * 查询某个读者的所有借阅记录
     */
    @Override
    public List<BorrowRecord> findBorrowRecordsByReaderId(int readerId) {
        List<BorrowRecord> borrowRecords = new ArrayList<>();
        String sql = "SELECT * FROM borrow_records WHERE reader_id = ?";
        try (Connection conn = DbUtils.getConnection();
             PreparedStatement pstmt = conn.prepareStatement(sql)) {
            pstmt.setInt(1, readerId);
            ResultSet rs = pstmt.executeQuery();
            while (rs.next()) {
                BorrowRecord record = new BorrowRecord(
                        rs.getInt("id"),
                        rs.getInt("reader_id"),
                        rs.getInt("book_id"),
                        rs.getTimestamp("borrow_date"),
                        rs.getTimestamp("return_date")
                );
                borrowRecords.add(record);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return borrowRecords;
    }
}
